package com.witdata.common.service.course.impl;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.CourseDto;
import com.witdata.common.domain.vo.CourseVo;
import com.witdata.common.domain.vo.UserVo;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.mapper.UserMapper;
import com.witdata.common.service.FileService;
import com.witdata.common.service.course.CourseService;
import com.witdata.common.util.IdUtils;
import com.witdata.mysql.base.service.CoursesBaseService;
import com.witdata.mysql.base.service.UserBaseService;
import com.witdata.mysql.base.service.CourseMembersBaseService;
import com.witdata.mysql.base.service.CourseSchedulesBaseService;
import com.witdata.mysql.domain.entity.TCourseMembers;
import com.witdata.mysql.domain.entity.TUser;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
@RequiredArgsConstructor
public class CourseServiceImpl implements CourseService {

    private final CoursesBaseService coursesBaseService;
    private final UserBaseService userBaseService;
    private final CourseMembersBaseService courseMembersBaseService;
    private final CourseSchedulesBaseService courseSchedulesBaseService;
    private final FileService fileService;

    @Override
    public Boolean createCourse(CourseDto courseDto) {
        return coursesBaseService.save(courseDto.toTCourses());
    }

    @Override
    public Boolean updateCourse(CourseDto courseDto) {
        // 判断课程是否存在
        if (ObjectUtils.isEmpty(courseDto.getId())) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = coursesBaseService.exists(new QueryWrapper().eq("id", courseDto.getId()));
        if (!exists) throw new GlobalException(Status.COURSENOTFOUND);
        // 更新课程
        return coursesBaseService.updateById(courseDto.toTCourses());
    }

    @Override
    public Boolean deleteCourse(Long courseId) {
        // 判断课程是否存在
        if (ObjectUtils.isEmpty(courseId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = coursesBaseService.exists(new QueryWrapper().eq("id", courseId));
        if (!exists) throw new GlobalException(Status.COURSENOTFOUND);
        // 删除课程
        return coursesBaseService.removeById(courseId);
    }

    @Override
    public CourseVo getCourseById(Long courseId) {
        // 判断课程是否存在
        if (ObjectUtils.isEmpty(courseId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = coursesBaseService.exists(new QueryWrapper().eq("id", courseId));
        if (!exists) throw new GlobalException(Status.COURSENOTFOUND);
        // 获取课程
        return new CourseVo(coursesBaseService.getById(courseId));
    }

    @Override
    public String uploadCover(MultipartFile cover) {
        return fileService.uploadCover(cover);
    }
}
